#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h> // Penser à inclure string.h pour strchr()

#define MAX_CMD 1000
#define ARG_LEN 100

void    get_paths(char** paths) {
    char*  str;
    char*  res;
    int    i = 0;

    str = getenv("PATH");
    res = strtok(str, ":");
    while (NULL != res) {
        paths[i] = res;
        res = strtok(NULL, ":");
        ++i;
    }
    paths[i] = NULL;
}

void    parse(char* cmd, char** args) {
    char *res;
    int  i = 0;

    res = strtok(cmd, " ");
    while (NULL != res) {
        args[i] = res;
        res = strtok(NULL, " ");
        ++i;
    }
    args[i] = NULL;
}
 
int     read_input(char *str, int length) {
    char *end = NULL;

    if (NULL != fgets(str, length, stdin)) {
        end = strchr(str, '\n');
        if (end != NULL) {
            *end = '\0';
        }
        return(1);
    }
    return(0);
}

char    *str_sub(const char *s, unsigned int start, unsigned int end) {
   char         *new_s = NULL;
   unsigned int i;

   if (NULL != s && start < end) {
      new_s = malloc(sizeof(*new_s) * (end - start + 2));
      if (NULL != new_s) {
         for (i = start; i <= end; i++) {
            new_s[i-start] = s[i];
         }
         new_s[i-start] = '\0';
      } else {
         fprintf (stderr, "Memoire insuffisante\n");
         exit(EXIT_FAILURE);
      }
   }
   return(new_s);
}

char*   file_exist(char *path, char *filename) {
    char *completpath;
    FILE *file = NULL;

    completpath = malloc(sizeof(*completpath) * (strlen(path) + strlen(filename) + 20));
    
    strcat(completpath, path);
    strcat(completpath, "/");
    strcat(completpath, filename);

    if ((file = fopen(completpath, "r"))) {
        fclose(file);
        return completpath;
    }
    return NULL;
}

int     main(int argc, char** argv, char** envp) {
    int   run = 1;
    int   i = 1; 
    char  cmd[MAX_CMD];
    char  pwd[MAX_CMD];
    char  *args[ARG_LEN];
    char  *paths[ARG_LEN];
    char  *completpath;
    pid_t pid;

    argc = argc;
    argv = argv;

    get_paths(paths);

    while (run) {
        printf("$?> ");      
        read_input(cmd, MAX_CMD);
        
        parse(cmd, args);

        if (strcmp(cmd, "pwd") == 0) {
            printf("%s\n", getcwd(pwd, MAX_CMD));
            continue;
        }
        if (strcmp(args[0], "cd") == 0) {
            chdir(args[1]);
            continue;
        }
        if (strcmp(args[0], "env") == 0) {
            while (NULL != envp[i]) {
                printf("%s\n", envp[i]);
                ++i;
            }
            continue;
        }
        if (strcmp(args[0], "exit") == 0) {
            return(1);
        }

        pid = fork();
        if (pid == 0) {
            while(paths[i] != NULL) {
                completpath = file_exist(paths[i], args[0]);
                if (NULL != completpath) {
                    args[0] = completpath;
                    if (execv(completpath, args) == -1) {
                        printf("ERROR : execv\n");
                    }
                }
                i++;
            }
        } else {
            wait(NULL);
        }      
    }
    return(1);
}

